<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/1/6
 * Time: 18:56
 */
namespace app\admin\controller;

use app\admin\model\School;
use think\Request;
class Schools extends Base{
    /**
     * 获取所有的单招学校
     */
    public function lists(){
        $user = $this->user_info();
        //查询所有的单招学校
        $res = School::order(["sort"=>"asc"])->select();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>$res,
        );
        return view("lists",$data);
    }
    //上传文件
    public function upload(Request $request){
        //接收上传的文件
        $file = $this->request->file('file');
        if(!empty($file)){
            //图片存的路径
            $imgUrl= ROOT_PATH . 'public' . DS .'static'. DS .'admin'. DS .'uploads'. DS .'schoollogo';

            // 移动到框架应用根目录/public/uploads/ 目录下

            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif'])->rule('uniqid')->move($imgUrl);
            $error = $file->getError();
            //验证文件后缀后大小
            if(!empty($error)){
                dump($error);exit;
            }
            if($info){
                // 成功上传后 获取上传信息

                //获取图片的名字
                $imgName = $info->getFilename();
                //获取图片的路径
                $photo= $imgName;

            }else{
                // 上传失败获取错误信息
                $file->getError();
            }
        }else{
            $photo = '';
        }
        if($photo !== ''){
            return ['code'=>1,'msg'=>'成功','photo'=>$photo];
        }else{
            return ['code'=>404,'msg'=>'失败'];
        }
    }
	/**
	 * 进入添加单招学校页面
	 */
	public function addform(){
		$user = $this->user_info();
		$data = array(
		    "setting" => $this->getAllSetting(),
		);
		return view("addform",$data);
	}
	/**
	 * 执行添加
	 * @param Request $request
	 */
	public function add(Request $request){
	    $user = $this->user_info();
	    $data = $request->post();
	    $data['update_time'] = time();
	    if(School::insert($data)){
	        $this->success("添加成功","Schools/lists");
	    }else{
	        $this->error("添加失败");
	    }
	}
     /**
     * 进入修改单招学校页面
     * @param Request $request
     */
    public function update(Request $request){
        $user = $this->user_info();
        $res = School::get($request->get("id"));
        $data = array(
            "setting" => $this->getAllSetting(),
            "data"=>$res,
        );
        return view("update",$data);
    }
    /**
     * 执行修改
     * @param Request $request
     */
    public function toUpdate(Request $request){
        $user = $this->user_info();
        $data = $request->post();
        $data['update_time'] = time();
        if(School::update($data)){
            $this->success("修改成功","Schools/lists");
        }else{
            $this->error("修改失败");
        }
    }
    //删除
    public function deletes(Request $request){
        $user = $this->user_info();
        if(Slideshow::destroy($request->post('id'))){
            $this->success("删除成功","Schools/lists");
        }else{
            $this->error("删除失败");
        }
    }
    //修改状态
    public function setStatus(Request $request){
        $data = $request->post();
        if(School::update($data)){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }
}